<?php

declare(strict_types=1);

namespace App\CustHub\Model;

use App\Common\Constants\ErrorCode;
use App\Exception\BusinessException;
use Mine\MineModel;

/**
 * @property int $id 主键
 * @property int $main_uid 主账号id
 * @property int $uid 私域用户ID
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @property string $title 任务标题
 * @property int $corp_id 企业id
 * @property array $contact_ids 用户ids
 * @property array $condition 客户筛选条件
 * @property int $contact_type 被邀请客户 1-所有客户 2-筛选客户 3-直接选择
 * @property int $is_guide 是否开启引导语
 * @property array $content 引导语
 * @property int $status -1 创建中 0等待中  1发送中  2已完成  3暂停中   4已取消  
 * @property string $start_time 
 * @property string $end_time 
 * @property array $crowd_conf 目标群配置
 * @property int $send_times 1立即发送  2定时发送
 * @property array $send_times_conf 发送时间配置
 * @property int $task_total 
 * @property int $complate_total 
 * @property-read null|ChUser $user 
 * @property-read null|ChCrowdInviteLog $record 
 */
class ChCrowdInvite extends MineModel
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'ch_crowd_invite';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'main_uid', 'uid', 'created_at', 'updated_at', 'title', 'corp_id', 'contact_ids', 'condition', 'contact_type', 'is_guide', 'content', 'status', 'start_time', 'end_time', 'crowd_conf', 'send_times', 'send_times_conf', 'task_total', 'complate_total'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'main_uid' => 'integer', 'uid' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'corp_id' => 'integer', 'contact_ids' => 'array', 'condition' => 'array', 'contact_type' => 'integer', 'is_guide' => 'integer', 'content' => 'array', 'status' => 'integer', 'crowd_conf' => 'array', 'send_times' => 'integer', 'send_times_conf' => 'array', 'task_total' => 'integer', 'complate_total' => 'integer'];

    public function user()
    {
        return $this -> hasOne(ChUser::class,'id','uid');
    }

    public function record()
    {
        return $this -> hasOne(ChCrowdInviteLog::class,'id','invite_id');
    }


    public static function saveInvite($uid,$main_uid,$data,$id = 0)
    {
        $model = new self();
        if($id){
            $model = self::where('id',$id) -> first();
            if(empty($model)) throw new BusinessException(ErrorCode::NOT_ISSET,'数据不存在');
        }
        $model -> uid = $uid;
        $model -> title = $data['title'];
        $model -> main_uid = $main_uid;
        $model -> corp_id = $data['corp_id'];
        $model -> contact_type = $data['contact_type'];

        if($data['contact_type'] == 3){
            $model -> contact_ids = is_array($data['contact_ids'])?$data['contact_ids']:json_decode($data['contact_ids'],true);
        }else if($data['contact_type'] == 2){
            $model -> condition = is_array($data['condition'])?$data['condition']:json_decode($data['condition'],true);
        }
        $model -> is_guide = $data['is_guide'];
        if($data['is_guide']){
            $model -> content = is_array($data['content'])?$data['content']:json_decode($data['content'],true);
        }
        $start_time=datetime(time());
        if($data['send_times'] == 2){
            $model -> send_times_conf = is_array($data['send_times_conf'])?$data['send_times_conf']:json_decode($data['send_times_conf'],true);
            $start_time = $model -> send_times_conf['time'];
        }
        $model -> start_time = $start_time;
        $model -> send_times = $data['send_times'];
        $model -> crowd_conf = is_array($data['crowd_conf'])?$data['crowd_conf']:json_decode($data['crowd_conf'],true);
        $model -> save();
        return $model;
    }
}
